<?php

namespace App\Http\Controllers\Admin;

use App\Common\Model\ShopPayUnline;
use App\Common\Service\TouchLayerService;
use App\Model\AixinPay;
use App\Model\Scores;
use App\Model\Setting;
use App\Service\MemberLevelService;
use App\Service\PeakReportService;
use App\Service\PointsService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ReportController extends Controller
{
    public $peakReportService;
    public $memberLevelService;
    public $touchLayerService;
    public $pointsService;
    public function __construct(PeakReportService $peakReportService,MemberLevelService $memberLevelService,TouchLayerService $touchLayerService,PointsService $pointsService)
    {
        $this->peakReportService = $peakReportService;
        $this->memberLevelService = $memberLevelService;
        $this->touchLayerService = $touchLayerService;
        $this->pointsService = $pointsService;
    }
    //用户的复投列表
    public function index(Request $request)
    {
        //从数据表中查询出数据
            $queryStr = $request->querystr;
            $datas = ShopPayUnline::from('shop_pay_unline as s')
                ->where('s.types',1)
                ->where(function ($query) use ($queryStr){
                    $query->where('s.num', 'like', $queryStr.'%')
                        ->orWhere('a.name','like',$queryStr.'%');
                })
                ->join('shop_accounts as a',function ($join){
                    $join->on('a.uid','=','s.uid');
                })
                ->join('member_levels as l','l.id','=','a.level')
                ->select('s.*','a.name as uname','l.level_money','l.level_name')
                ->orderby('s.create_time','desc')
                ->paginate(20);
            return view('admin.repeat.index',['datas'=>$datas,'querystr'=>$queryStr]);
    }
    /*
     * 用户的复投审核
     */
    public function verifyStatus(Request $request)
    {
        DB::beginTransaction();
        try{
            //操作shop_pay_Unline表
            $rs1 = ShopPayUnline::where('id', $request->id)
                    ->update(['status' => $request->status,'update_time'=>time()]);
            //操作aixin_pay表
                $data = [
                    'channel'=>1,   //支付渠道为线下‘1’
                    'uid'=>$request->uid,
                    'money'=>$request->money,
                    'status'=>1,    //状态为1，支付完成
                    'pay_time'=>$request->paytime,
                    'create_time'=>$request->paytime,
                ];
            $rs2 = AixinPay::create($data);
            //操作积分表
                $mTs = new Setting();
                $proportion = $mTs->getMoneyToScoresSet();
                $bill = $request->money*$proportion;
                $data1 = [
                    'uid'=>$request->uid,
                    'bill'=>$bill,
                    'remark'=>'复投积分',
                    'create_time'=>time()
                ];
            $rs3 = Scores::create($data1);
            //初始化用户收益额度>>>操作peak_report表,status为1表示审核成功
            $total = $this->memberLevelService->compute($request->uid);
            $rs4 = $this->peakReportService
                ->addUserPeak($request->uid,$request->paytime,$total,'复投后增加收益额度',1);
            if ($rs1&& $rs2 && $rs3 && $rs4 ){
                DB::commit();
                //层碰
                $rs5 = $this->touchLayerService->collisionLayer($request->uid);
                if(!$rs5){
                    Log::info('层碰error:'.$this->touchLayerService->getError());
                }
                //量碰
                $this->pointsService->addAchievement($request->uid,$request->money);
            }
        }catch(\Exception $e){
            DB::rollBack();
            return ApiResponse([], '修改失败', 1);
        }
        return ApiResponse([]);
    }
}
